package com.example.demo.dao;

import com.example.demo.pojo.TestPaper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface DaoTestPaper extends JpaRepository<TestPaper,String> , QueryByExampleExecutor<TestPaper> {
    Page<TestPaper> findAllByClassNameContainingOrTestNameContaining(String s, String n, Pageable p);

    int countByClassNameContainingOrTestNameContaining(String s, String n);

    TestPaper findByTestId(String id);

    @Query(nativeQuery=true, value = "select test_paper.* from test_paper left join class_user " +
            "on test_paper.class_id=class_user.class_id where (user_id=:id and test_paper.types=1 and (test_paper.test_name like %:name% or test_paper.class_name like %:name%))")
    Page<TestPaper> getUserTest(@Param("name") String name, @Param("id") String userId,Pageable p);
    @Query(nativeQuery=true, value = "select COUNT(*) from test_paper left join class_user " +
            "on test_paper.class_id=class_user.class_id where (user_id=:id and test_paper.types=1 and (test_paper.test_name like %:name% or test_paper.class_name like %:name%))")
    Long countUserTest(@Param("name") String name, @Param("id") String userId);
    @Query(nativeQuery = true,value = "select * from test_paper ")
    List<TestPaper> getAll();

}

